﻿using CK.Common;
using CK.Project.Core6.Core;
using CK.Project.Core6.EntityFramework;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CK.Project.Core6.Application
{
    /// <summary>
    /// 用户模块接口实现类
    /// </summary>
    public class UserAppService : IUserAppService
    {
        /// <summary>
        /// 用户
        /// </summary>
        private readonly PgRepository<User> userRepository;

        public UserAppService(PgRepository<User> userRepository)
        {
            this.userRepository = userRepository;
        }

        /// <summary>
        /// 查询用户列表
        /// </summary>
        /// <param name="userListDto"></param>
        /// <returns></returns>
        public OUserListDto UserList(UserListDto userListDto)
        {
            //实例方法输出对象
            OUserListDto oUserListDto = new OUserListDto();

            var where = Extend<User>.TrueExpr;

            //姓名
            if (!userListDto.userName.IsNullOrEmpty())
            {
                where = where.And(a => a.user_name.Contains(userListDto.userName));
            }

            var listSource = userRepository.GetPageList(where
                ,new PageModel { PageIndex = userListDto.PageIndex,PageSize = userListDto.PageSize}) //根据分页查询
                .OrderBy(a => a.datains_time).ToList() //根据数据插入时间进行正序排序
                .CopyToModel<User,Temp_User>(); //将数据复制到临时实体类

            oUserListDto.listSource = listSource;
            oUserListDto.total = userRepository.Count(where);
            oUserListDto.OK();

            return oUserListDto;
        }
    }
}
